package com.zjitc.service.imp;

import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by LWW on 2018/3/29.
 */
@Service
public class UserServiceImp implements UserService{

    @Autowired
    private UserDao userDao;

    @Override
    public ServerResponse<User> login(String username, String password) {
        int count = userDao.userNameCount(username);
        if (count != 1) {
            return ServerResponse.createError("user do not exist");
        }

        // Todo. password 需要加密
        User user = userDao.findByUserNameAndPassword(username, password);
        if (user == null) {
            return ServerResponse.createError("password error");
        }

        user.setPassword(null);
        user.setQuestion(null);
        user.setAnswer(null);
        return ServerResponse.createSuccess(user);
    }

  @Override
  public ServerResponse<User> register(User user) {
    int count =userDao.userNameCount(user.getUsername());
    if(count !=0){
      return ServerResponse.createError("用户已存在");
    }
    int addUser=userDao.add(user);
    if(addUser!=1){
      return ServerResponse.createError("添加失败");
    }

    return ServerResponse.createSuccess("校验成功");
  }

  @Override
  public ServerResponse<User> check(String str, String type) {
      if(type.equals("username")){
        int count=userDao.userNameCount(str);
        if(count ==1){
          return ServerResponse.createError("用户已存在");
        }else{
          return ServerResponse.createSuccess("校验成功");
        }
      }else if(type.equals("email")){
        int count=userDao.userNameCount(str);
        if(count ==1){
          return ServerResponse.createError("用户已存在");
        }else{
          return ServerResponse.createSuccess("校验成功");
        }
      }
      return ServerResponse.createSuccess("校验成功");
  }

  @Override
  public ServerResponse<User> findAllUser(HttpSession session) {
      User user= (User) session.getAttribute("user");
      if(user==null){
        return ServerResponse.createError("用户未登录,无法获取当前用户信息");
      }else {
        int count=userDao.userNameCount(user.getUsername());
        if(count !=1){
          return ServerResponse.createError("不存在该用户");
        }
      }
      user.setPassword(null);
      user.setQuestion(null);
      user.setAnswer(null);
      return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse<User> forget(User user) {
    int count=userDao.userNameCount(user.getUsername());
    String q=userDao.question(user.getUsername());
    if(count==0){
      return ServerResponse.createError("用户不存在");
    }else{
      if(q==null){
        return ServerResponse.createError("该用户未设置找回密码问题");
      }
    }
    return ServerResponse.createSuccess(q);
  }
}
